package cn.com.shopec.erp.multiChannel.dao.impl;

import java.math.BigInteger;
import java.util.List;

import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import cn.com.shopec.erp.common.orm.impl.HibernateEntityDao;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.page.PageQuery;
import cn.com.shopec.erp.common.utils.LogicUtil;
import cn.com.shopec.erp.common.utils.StringUtil;
import cn.com.shopec.erp.multiChannel.dao.IProductPackageItemDao;
import cn.com.shopec.erp.multiChannel.entity.ProductPackageItem;
import cn.com.shopec.erp.multiChannel.vo.ChannelOrderVo;

@Repository
public class ProductPackageItemDaoImpl extends HibernateEntityDao<ProductPackageItem>
		implements IProductPackageItemDao {
	@Override
	public PageFinder productPackageItemPaged(ChannelOrderVo channelOrderVo, PageQuery pageQuery) {
		String params = "";
		if (channelOrderVo != null) {
			if (StringUtil.isExist(channelOrderVo.getProdPackageNo())) {
				params += " AND ppi.prod_package_no ='" + channelOrderVo.getProdPackageNo() + "'";
			}
		}
		String querysql = "select ppi.id,ppi.prod_package_no prodPackageNo,ppi.prod_no prodNo,ppi.prod_name prodName,"
				+ "ppi.price,ppi.total,ppi.wh_no whNo,ppi.sup_no supNo,ppi.state,w.wh_name whName,si.sup_name supName,"
				+ "si.code code,si.sup_code supCode,p.prod_bar_code prodBarCode,"
				+ "(select GROUP_CONCAT(svo.option_name) option_name from tbl_spec_val_option svo "
				+ "left outer join tbl_prod_spec_value psv "
				+ "on svo.option_no=psv.val_option_no where psv.prod_no=ppi.prod_no limit 0,1) specName,p.commo_no commoNo "
				+ "from tbl_product_package_item ppi " 
				+ "left join tbl_warehouse w on ppi.wh_no=w.wh_no "
				+ "left join tbl_product p on ppi.prod_no = p.prod_no "
				+ "left join tbl_supplier_info si on ppi.sup_no=si.code where 1=1 " + params;
		String countsql = "select count(*) from tbl_product_package_item ppi where 1=1 " + params;
		BigInteger count = (BigInteger) this.createSQLQuery(countsql).list().get(0);
		List list = getSession().createSQLQuery(querysql).setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP)
				.setFirstResult((pageQuery.getPage() - 1) * pageQuery.getPageSize())
				.setMaxResults(pageQuery.getPageSize()).list();
		PageFinder page = new PageFinder(pageQuery.getPage(), pageQuery.getPageSize(), count.intValue(), list);
		return page;
	}

	@Override
	public int updateProductPackageItem(ProductPackageItem productPackageItem) {
		String params = "";
		if (LogicUtil.isNotNull(productPackageItem.getState())) {
			if (StringUtil.isExist(params)) {
				params += ",state='" + productPackageItem.getState() + "'";
			} else {
				params += " state='" + productPackageItem.getState() + "'";
			}
		}
		if (StringUtil.isExist(params)) {
			String sql = "update tbl_product_package_item set " + params + " where prod_package_no='"
					+ productPackageItem.getProdPackageNo() + "'";
			return this.createSQLQuery(sql).executeUpdate();
		} else {
			return 0;
		}
	}
}
